//
//  ViewController.m
//  ManageUtil2018
//
//  Created by zhuzj on 2018/4/9.
//  Copyright © 2018年 zhuzj. All rights reserved.
//

#import "ViewController.h"
#import "ZHeader.h"
#import "NSString+URL.h"
#import "TestView.h"

@interface ViewController ()

@property (nonatomic, strong) TestView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self testYYlabel];
//    [self testFile];
//    if(IS(ViewController, [ViewController new]))
//    {
//        NSLog(@"ViewController");
//    }
//    NSLog(@"%@",CLASS_NAME(ViewController));
//
//    NSLog(@"%@",AS(ViewController, [ViewController new]));
    
    [self testPerformSelectorReturnValue];
    
    NavBarHeight;
}

- (void)testYYlabel
{
    UrlLabel *urlLabel = [[UrlLabel alloc] initWithFrame:CGRectMake(0, 300, self.view.width, 50)];
    [self.view addSubview:urlLabel];
    
    UrlLabel *urlLabel1 = [[UrlLabel alloc] initWithFrame:CGRectMake(0, 350, self.view.width, 50)];
    [self.view addSubview:urlLabel1];
    
    NSString *text = @"我的Url：https://www.baidu.com 自定义：自定义1，自定义2。哈哈test";
    UIColor *textColor = UIColor.blackColor;
    UIFont * font = [UIFont systemFontOfSize:15.0];
    UIColor *urlColor = UIColor.blueColor;
    [urlLabel setText:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font urlColor:(UIColor *)urlColor urlTapBlock:^(NSURL * _Nonnull url) {
        NSLog(@"urlTapBlock");
    }];
    
    NSArray *customStrs = @[@"自定义1", @"自定义2"];
    NSMutableArray *rangesM = [NSMutableArray new];
    for (NSString *str in customStrs) {
        NSRange range = [text rangeOfString:str];
        [rangesM addObject:NSStringFromRange(range)];
    }
    UIColor *customColor = UIColor.yellowColor;
    UIFont * customFont = [UIFont systemFontOfSize:18.0];
    [urlLabel1 setText:text textColor:textColor font:font urlColor:urlColor textAligment:NSTextAlignmentLeft customRanges:[rangesM copy] customTapAction:^(NSAttributedString * _Nonnull text, NSRange range, NSAttributedString * _Nonnull customText) {
        NSLog(@"customTapAction:%@",customText.string);
    } customLongPressAction:^(NSAttributedString * _Nonnull text, NSRange range, NSAttributedString * _Nonnull customText) {
        NSLog(@"customLongPressAction:%@",customText.string);
    } customColor:customColor customFont:customFont lineSpacing:9.0 urlTapBlock:^(NSURL * _Nonnull url) {
        NSLog(@"urlTapBlock");
    }];
}

- (void)testFile
{
    [CacheHelper createFolderWithFolderName:@"folderName"];
    [CacheHelper createFolderWithFolderPath:CachePath];
    
    [CacheHelper deleteOverDueAppFolder:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)btnClick:(id)sender {
    
    NSLog(@"btnClick");
    
    NSURL *urlII = [NSURL URLWithString:@"fbee://"];
    BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:urlII];
    if (hadInstalledWeixin)
    {
        
        NSLog(@"说明此设备有安装app");
        //打开APP
        if (@available(iOS 10, *)) {
            [UIApplication.sharedApplication openURL:urlII options:@{} completionHandler:nil];
        } else {
            [UIApplication.sharedApplication openURL:urlII];
        }
        
    }else{
        
        NSLog(@"说明此设备没有安装app");
    }
}
- (IBAction)testUrlAnalysis:(id)sender {
    
    //NSString *urlStr = @"fbee://jump.fbee.one/topic/test/?key=b8ca8275e301464a99b2e1a484a2aa0f&key1=0e6db54b065b4d669842b1e5260bee10&key2=https://www.fbee.site/signed?gid=6e1655d2e17f489eae57b565b9e275b7";
    NSString *urlStr = @"fbee://jump.fbee.one/topic/test/中国?key=b8ca8275e301464a99b2e1a484a2aa0f&key1=0e6db54b065b4d669842b1e5260bee10&key2=中国&key3=https://www.fbee.site/signed?gid=6e1655d2e17f489eae57b565b9e275b7";
    
    
    NSDictionary *dic =  [NSString getDictByRequstStrNSUrl:urlStr];
    NSDictionary *dic1 =  [NSString getDictByRequstStrNSUrl1:urlStr];
    NSDictionary *dic2 =  [NSString getDictByRequstStrString:urlStr];
    
    NSLog(@"dic = %@",dic);
    NSLog(@"dic1 = %@",dic1);
    NSLog(@"dic2 = %@",dic2);
}

- (void)testPerformSelectorReturnValue {
    
    NSString *str = @"123";
    NSNumber *num = @123;
    
    NSString *strNum1 = SafeGetStringValue(num);
    NSString *strNum2 = [num performSelector:@selector(stringValue)];
    
    NSInteger num1 = SafeGetIntValue(str);
    NSInteger num2 = (int)[str performSelector:@selector(intValue)];
    
    BOOL boolNum = SafeGetBOOLValue(str);
    int intNum = SafeGetIntValue(str);
    float floatNum = SafeGetFloatValue(str);
    float doubleNum = SafeGetDoubleValue(str);

    
    TestView *view = [TestView new];
    self.testView = view;
    [self addObservers];
    view.allWidth = 100;
    view.methodName = @"methodName";
    if([view respondsToSelector:@selector(allWidth)]){
        NSLog(@"%@",[view performSelector:@selector(allWidth)]);
    }
    NSLog(@"%lf",view.allWidth);
    
    NSLog(@"boolNum:%d##intNum:%d##floatNum:%lf##doubleNum:%lf",boolNum,intNum,floatNum,doubleNum);
}

#pragma mark - Observer KVO
- (void)addObservers {
    [self.testView addObserver:self forKeyPath:NSStringFromSelector(@selector(methodName)) options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}

- (void)removeObservers {
    [self.testView removeObserver:self forKeyPath:NSStringFromSelector(@selector(methodName))];
    if (self && self.testView.observationInfo) {//判断接收者的observationInfo
        @try {
            //NSLog(@"observationInfo:%@",self.testView.observationInfo);
            [self.testView removeObserver:self forKeyPath:NSStringFromSelector(@selector(methodName))];
        } @catch (NSException *exception) {
            
        }
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(methodName))]){
        id newValue = change[@"new"];
        id oldValue = change[@"old"];
        NSLog(@"newValue:%@,oldValue:%@",newValue,oldValue);
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end
